from app import db
from datetime import datetime

class Environment(db.Model):
    __tablename__ = 'environments'
    
    id = db.Column(db.Integer, primary_key=True)
    project_id = db.Column(db.Integer, db.ForeignKey('projects.id'), unique=True, nullable=False)
    temperature_range = db.Column(db.String(64))  # 温度范围
    humidity_range = db.Column(db.String(64))     # 湿度范围
    electromagnetic = db.Column(db.Text)          # 电磁环境
    layout = db.Column(db.Text)                   # 布局要求
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    def __init__(self, project_id, temperature_range=None, humidity_range=None,
                 electromagnetic=None, layout=None):
        self.project_id = project_id
        self.temperature_range = temperature_range
        self.humidity_range = humidity_range
        self.electromagnetic = electromagnetic
        self.layout = layout
    
    def __repr__(self):
        return f'<Environment {self.project_id}>'
    
    def to_dict(self):
        return {
            'id': self.id,
            'project_id': self.project_id,
            'temperature_range': self.temperature_range,
            'humidity_range': self.humidity_range,
            'electromagnetic': self.electromagnetic,
            'layout': self.layout,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None
        } 